Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
Alternative of "if statement" with "switch statement

I'm currently write the litte Scissors, paper, rock game and I used "If statement" to do so. Now, I try to write it with the "switch statement". So I will show you what I wrote but nothing happen in my console and I don't know why. Seems pretty easy for anybody used to code everyday I think.

So I begin to declare my variables.

let rock = 'rock';
let scissors = 'scissors';
let paper = 'paper';

then these two variables will be changed to test the conditional

let firstPlayerChoice = paper;
let secondPlayerChoice = rock;

After that I made my seven possibilities

let firstPossibility = firstPlayerChoice === paper && secondPlayerChoice === rock;
let secondPossibility = firstPlayerChoice === scissors && secondPlayerChoice === paper;
let thirdPossibility = firstPlayerChoice === rock && secondPlayerChoice === scissors;

let fourthPossibility = firstPlayerChoice === rock && secondPlayerChoice === paper;
let fifthPossibility = firstPlayerChoice === paper && secondPlayerChoice === scissors;
let sixthPossibility = firstPlayerChoice === scissors && secondPlayerChoice === rock;

let seventhPossibility = firstPlayerChoice === secondPlayerChoice;

So, now I create my condition using my Switch statement but nothing is running on my console. The fact is I have difficulties to identify the good expression.

switch (firstPlayerChoice) {
    case firstPlayerChoice === paper && secondPlayerChoice === rock:
    case firstPlayerChoice === scissors && secondPlayerChoice === paper:
    case firstPlayerChoice === rock && secondPlayerChoice === scissors:
        console.log('First Player won !');
        break;
}

switch (secondPlayerChoice) {
    case fourthPossibility:
    case fifthPossibility:
    case sixthPossibility:
        console.log('Second player won !');
        break;
}

Someone can provide me just one expression to help to understand the big picture please ?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda